package com.newedu.equipmgr.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author jerry
 * @create 2020-06-10 15:48:48
 * @company 新开普电子股份有限公司
 *
 * 字符编码处理过滤器
 *
 * tomcat8.0以上的逻辑
 * 当request.method = post 的时候，[默认tomcat采用的iso8859-1的编码方式，不能处理中文的显示]我们为了不产生乱码
 * 获取参数前，我们需要设置request的编码方式
 *  request.setCharacterEncoding("utf-8");
 *
 *  每次response的时候，[默认tomcat采用的iso8859-1的编码方式，不能处理中文的显示]为了不产生乱码
 *  每次都需要设置编码方式
 *  response.setContentType("application/json;charset=utf-8");
 */
@WebFilter(filterName = "EncodingFilter", urlPatterns = {"/api/servlet/*","/servlet/*"})
public class EncodingFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("Encoding is working");
//        req.getMethod();
//        ServletRequest 需要向下类型转换
        HttpServletRequest request = (HttpServletRequest)req;
        // 判断请求的方式为post时，需要指定utf-8编码方式
        if(request.getMethod().equalsIgnoreCase("post")){
            // 对request添加字符编码utf-8
            request.setCharacterEncoding("utf-8");
        }
        System.out.println("请求的queryString:"+request.getQueryString());
        System.out.println("请求的ServletPath:"+request.getServletPath());
        System.out.println("请求的RequestURI:"+request.getRequestURI());

        // 对response设置编码方式
        // text/html;charset=utf-8 返回html页面
        // application/json;charset=utf-8 返回json数据
        resp.setContentType("application/json;charset=utf-8");

        // 放行用户的请求
        chain.doFilter(request, resp);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
